﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;

namespace T9Silverlight.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class T9Service
    {
        private static readonly T9.T9Engine _engine = new T9.T9Engine();

        [OperationContract]
        public void SetUpEngine(string dictionaryName)
        {

        }//end SetUpEngine
        [OperationContract]
        public List<String> GetWordPossibilities(string digitString)
        {
            var wordList = new List<String>();
            var prefixList = new List<String>();
            _engine.GetWordsAndPrefixes(digitString, out wordList, out prefixList);
            if (wordList.Any())
            {
                var result = new List<string>{digitString};
                result.AddRange(wordList);
                return result;
            }//end if
            else if (prefixList.Any())
            {
                var result = new List<string> { digitString };
                result.Add(prefixList.First());
                return result;
            }
            else
                throw new Exception("The T9 Engine did not return any valid words or prefixes");
        }//end GetWordPossibilities

        // Add more operations here and mark them with [OperationContract]
    }

}
